import Vue from 'vue'
import store from "../store/index.js"

// const API = 'https://test.etc.link' // 测试
const API = 'https://wx.etc.link' // 正式

	
const quest = (url, method, data,header) => {
	
	var shenglueApplytoken = uni.getStorageSync("shenglueApplytoken")
	let httpDefaultOpts = {
		url: API + url,
		data: data,
		method: method,
		header: {
			'content-type': 'application/json',
			'apply-secret': 'base64:szoJ3mSxU7zOsJfU7s4pSahi43wdh01x6badmz5FtCM=',
			'Authorization': 'Bearer ' + shenglueApplytoken,
			...header
		},
		dataType: 'json',
	}

	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				
				if(res[1].statusCode==200){
					if(res[1].data.code==200 || res[1].data.code==1  ){
						resolve(res[1].data)
						// console.log(res[1].data)
					}else if(res[1].data.code==50007){
						// console.log('50007')
						uni.setStorageSync('applyShenglueSession_key', '');
						uni.setStorageSync('applyShenglueOpenid', '');
						uni.setStorageSync('shenglueApplytoken', '');
						uni.setStorageSync('userInfo', '');
						store.commit('changeUserInfo',{})
						uni.setStorageSync("logincode",'');
						// 重新登录 获取  code
						uni.login({
						    success:res=>{
						        uni.setStorageSync("logincode",res.code);
						    },
						    fail:err=>{
						        uni.setStorageSync("logincode",'');
						    }
						})
						uni.showToast({
							icon: 'none',
							title: res[1].data.message , 
							duration: 1000
						});
						resolve(res[1].data)
						
					}else{
						uni.showToast({
							icon: 'none',
							title: res[1].data.message , 
							duration: 1000
						});
						resolve(res[1].data)
					}
				}else{
					uni.showToast({
						icon: 'none',
						title: res[1].data.message , 
						duration: 2000
					});
					reject(res[1].data)
				}
			}
		).catch(
			(response) => {
				uni.showToast({
					title: '网络连接失败',
					icon: 'none'
				})
				reject(response)
			}
		)
	})
	return promise
};

// token 
const questToken = (url, method, data) => {
	let httpDefaultOpts = {
		url: API + url,
		data:  Object.assign(data, {token:uni.getStorageSync('token')}),
		method: method,
		header: {
			"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
		},
		dataType: 'json',
	}

	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				if  (res[1].data.code == 1) {
					resolve(res[1].data)
				} else if (res[1].data.code == 10) {
					uni.showToast({
						title: res[1].data.resule,
						icon: 'none',
						duration: 2000
					})
					setTimeout(()=>{uni.reLaunch({url:'/pages/login/login'})},800)
				} else {
					uni.showToast({
						title: res[1].data.resule,
						icon: 'none',
						duration: 2000
					})
					reject(res[1].data)
				}
			} 
		).catch(
			(response) => {
				// console.log(response)
				uni.showToast({
					title: '服务器繁忙,请稍后再试',
					icon: 'none'
				})
				reject(response)
			}
		)
	})
	return promise
};

export default {
	quest,
	API,
	questToken
}


